<?php
include("../../product/model.php");

// ---------------------------------
// controller 
// ---------------------------------
// list every products by (type, page_num, list_per_page)
// which define by url GET method
$type = NULL;
$page = NULL;
$list_per_page = 20;
$file_name = $_SERVER["PHP_SELF"];

if (array_key_exists("type", $_GET))
{
	$type = $_GET["type"];
}
if (array_key_exists("page", $_GET))
{
	$page = $_GET["page"];
}
else
{
	$page = 1;
}

$product_list = model_get_product_list($type, $page, $list_per_page);
array_walk($product_list, "set_edit_page");
array_walk($product_list, "set_delete_page");
$body_html = view_product_list($product_list);

$page_count = model_get_page_count($type, $list_per_page);
$body_html = $body_html . view_page_list($file_name, $page, $page_count, $type);

include("../template/layout.php");

// ---------------------------------
// model
// ---------------------------------
function set_edit_page(&$value, $key)
{
	$value["edit_page"] = "edit.php?id=" . $value["id"];
}

function set_delete_page(&$value, $key)
{
	$value["delete_page"] = "delete.php?id=" . $value["id"];
}

// ---------------------------------
// view
// ---------------------------------
function view_product_list($product_list)
{
	$product_list_html = "";
	$product_list_html = "<!--product list start-->\n";
	$product_list_html = $product_list_html . "<div class='product_list'>\n";
	foreach ($product_list as $product)
	{
		$product_list_html = $product_list_html .
		view_product(
		$product["small_image"],
		$product["name"],
		$product["model"],
		$product["specification"],
		$product["market_price"],
		$product["promotional_price"],
		$product["click_count"],
		$product["edit_page"],
		$product["delete_page"]
		);
	}
	$product_list_html = $product_list_html . "</div>\n";
	$product_list_html = $product_list_html . "<!--product list end-->\n";
	return $product_list_html;
}

function view_product($picture, $name, $model, $specification, $market_price, $promotional_price, $click_count, $edit_page, $delete_page)
{
	$product_html =
	sprintf("<table class='product_intro'>
		<tr> <td class='product_picture' rowspan='9'><img src='%s'></div> </tr>
		<tr> <td class='product_name'>%s</td></tr>
		<tr> <td class='product_model'>型号:%s</td> </tr>
		<tr> <td class='product_specification'>规格:%s</td> </tr>
		<tr> <td class='product_market_prcie'>市场价:%s</td> </tr>
		<tr> <td class='product_promotional_price'>优惠价:%s</td> </tr>
		<tr> <td class='product_click_count'>点击数:%d</td> </tr>
		<tr> <td class='product_edit'><a href='%s'>编辑</a></td> </tr>
		<tr> <td class='product_delete'><a href='%s'>删除</a></td> </tr>
		</table>",
		'../../upload/' . $picture,
		$name,
		$model,
		$specification,
		$market_price,
		$promotional_price,
		$click_count,
		$edit_page,
		$delete_page
	);
	return $product_html;
}

function view_page_list($file_name, $page, $page_count, $type)
{
	$page_list_html = "";
	// 如果需要，显示上一页
	if ($page > 1)
	{
		if ($type)
		{
			$page_list_html = $page_list_html . sprintf("<a href=\"". $file_name ."?page=%d&type=%d\">上一页</a>", $page - 1, $type);
		}
		else
		{
			$page_list_html = $page_list_html . sprintf("<a href=\"". $file_name ."?page=%d\">上一页</a>", $page - 1);
		}
	}
	// 如果需要，显示下一页
	if ($page + 1 < $page_count)
	{
		if ($type)
		{
			$page_list_html = $page_list_html . sprintf("<a href=\"". $file_name ."?page=%d&type=%d\">下一页</a>", $page + 1, $type);
		}
		else
		{
			$page_list_html = $page_list_html . sprintf("<a href=\"". $file_name ."?page=%d\">下一页</a>", $page + 1);
		}
	}
	$page_list_html = $page_list_html . sprintf("%d/%d", $page, $page_count);
	return $page_list_html;
}
?>
